<template>
  <div class="repair">
    <!-- 位置显示 -->
    <div class='position'>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/repair' }">报修管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 1. 搜索区域 -->
    <div class="header">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="报修人名称">
          <el-input v-model="formInline.name" placeholder="请输入报修人姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="findAccount()">查询</el-button>
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="openAddDialog()" v-if="this.role == 3">我要报修</el-button>
          <template>
            <download-excel class="export-excel-wrapper" :data="tableData" name="repair.xls">
              <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
              <el-button type="primary" icon="el-icon-download">导出数据</el-button>
            </download-excel>
          </template>
        </el-form-item>
      </el-form>
    </div>

    <!-- 2. 表格区域展示视图数据 -->
    <div class="wrapper">
      <el-table :data="tableData" border>
        <el-table-column prop="rid" label="id" sortable width="70"></el-table-column>
        <el-table-column prop="student" label="报修人" width="80">
          <template slot-scope="scope">
            <span>{{scope.row.student.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="报修时间" width="auto"></el-table-column>
        <el-table-column prop="title" label="报修内容" width="100"></el-table-column>
        <el-table-column prop="dormitoryManager" label="处理人" width="auto">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.dormitoryManager==null">暂未处理</el-tag>
            <span v-else>{{scope.row.dormitoryManager.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="handleTime" label="处理时间" width="auto">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.handleTime==null">暂未处理</el-tag>
            <span v-else>{{scope.row.handleTime}}</span>
          </template>
        </el-table-column>

        <el-table-column prop="updateTime" label="修改时间" width="auto"></el-table-column>
        <el-table-column prop="version" label="修改次数" width="80"></el-table-column>
        <el-table-column prop="status" label="信息状态" width="80">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="findDate(scope.row)">查看</el-button>
            <el-button type="primary" size="mini" v-if="scope.row.status==true &&(role == 1 || role == 2)" @click="handleEdit(scope.row)" icon="el-icon-edit">
              处理</el-button>
            <el-button size="mini" type="danger" v-if="scope.row.status==true &&(role == 1 || role == 2) " @click="handleDelete(scope.row)" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 3. 分页 -->
   <MyPagination :total="total" :pageSize="pageSize" @SizeChange="SizeChange" @Pagechange="Pagechange" :currentPage="currentPage" />
    <!-- 查看数据 -->
    <el-dialog title="查看入住信息" :visible.sync="MessageDalog" width="70%" :before-close="handleClose">
      <el-table :data="rowData">
        <el-table-column prop="student" label="报修人" width="80">
          <template slot-scope="scope">
            <span>{{scope.row.student.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="报修时间" width="auto"></el-table-column>
        <el-table-column prop="title" label="报修内容" width="100"></el-table-column>
        <el-table-column prop="dormitoryManager" label="处理人" width="auto">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.dormitoryManager==null">暂未处理</el-tag>
            <span v-else>{{scope.row.dormitoryManager.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="handleTime" label="处理时间" width="auto">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.handleTime==null">暂未处理</el-tag>
            <span v-else>{{scope.row.handleTime}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="version" label="修改次数" width="80"></el-table-column>
        <el-table-column prop="status" label="信息状态" width="80">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog title="我要报修" :visible.sync="addDialog">
      <el-form :model="addfrom">
        <el-form-item label="报修内容" label-width="120px">
          <el-input type="textarea" v-model.trim="addfrom.title"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddRepair()">取 消</el-button>
        <el-button type="primary" @click="handleAddRepair()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { mapState } from "vuex"
import {
  GetPageAllRepair, addRepair, updateRepair, deleteRepair
} from '@/api/api'
import MyPagination from "../components/MyPagination.vue";
export default {
  components: {
    MyPagination,
  },
  computed: {
    ...mapState(['role', 'userinfo']),
  },
  data() {
    return {
      formInline: {
        name: "",
      },
      tableData: [],
      total: 5,
      pageSize: 5,
      currentPage: 1, //选中的高亮页码
      //从这开始是查看数据的暂用变量
      MessageDalog: false,
      rowData: [],//行数据
      //从这开始是添加数据的暂用变量
      addDialog: false,
      addfrom: {
        title: ''
      }
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      GetPageAllRepair({ pageSize: this.pageSize, pageNum: this.currentPage, name: this.formInline.name }).then((result) => {
        console.log(result);
        if (result.status == 200) {
          this.tableData = result.data.list
          this.total = result.data.total
          this.pageSize = result.data.pageSize
          this.currentPage = result.data.pageNum
        } else {
          this.$message.error(result.msg)
        }
      })
    },
    findAccount() {
      if (this.formInline.name == '') return this.$message.error('输入数据为空无法查询')
      GetPageAllRepair({ pageSize: this.pageSize, pageNum: this.currentPage, name: this.formInline.name }).then((result) => {
        console.log(result);
        if (result.status == 200) {
          this.$message.success(result.msg)
          this.tableData = result.data.list
          this.total = result.data.total
          this.pageSize = result.data.pageSize
          this.currentPage = result.data.pageNum
        } else {
          this.$message.error(result.msg)
        }
      })
      this.formInline.name = ''
    },
    Pagechange(pageNum) {//改变页码
      console.log(pageNum);
      this.currentPage = pageNum
      this.loadData()
    },
    SizeChange(pageSize) {//改变一页有几条
      console.log(pageSize);
      this.pageSize = pageSize
      this.loadData()
    },
    findDate(row) {//打开每行数据dialog
      console.log(row);
      this.rowData.push(row)//添加一行数据到rowdata变量
      this.MessageDalog = true//显示对话框
    },
    handleClose() {//关闭对话框
      this.MessageDalog = false
      this.rowData = []//清空数据
    },
    openAddDialog() {//打开新增dialog
      this.addDialog = true
    },
    cancelAddRepair() {//关闭dialog 并且清空输入框
      this.$message.info('取消新增')
      this.addDialog = false
      this.addfrom.title = ''
    },
    submitAddFrom() {
      if (this.addfrom.title === '') return this.$message.error('请选择完整的信息')
      console.log(this.addfrom);
      this.handleAddLive()
    },
    handleAddRepair() {//新增请求接口
      if (this.role != 3) return this.$message.error('只有学生用户才可以报修登记哦!')
      addRepair({ sid: this.userinfo.sid, title: this.addfrom.title }).then(res => {
        console.log(res);
        if (res.status == 200) {
          this.$message.success(res.msg)
          this.addfrom.title = ''
          this.addDialog = false
          this.loadData()
        } else {
          return this.$message.error(res.msg)
        }
      })
    },
    handleEdit(data) {//处理报修
      if (this.role != 2) return this.$message.error('只有宿管用户才可以处理登记哦!')
      if(data.dormitoryManager !=null) return  this.$message.error('该登记已被处理!')
      //请求后端处理
      this.$confirm('确认处理?')
        .then(_ => {
          updateRepair({ did: this.userinfo.id, id: data.rid }).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message.success(res.msg)
              this.updateDialog = false
              this.loadData()
            } else {
              this.$message.error(res.msg)
            }
          })
        })
        .catch(_ => {
          this.$message.info('您选择取消处理')
        });
    },
    handleDelete(data) {//删除
      console.log(data);
      this.$confirm('确认删除此信息吗?')
        .then(_ => {
          deleteRepair({ id: data.rid }).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message.success(res.msg)
              this.loadData()
            } else {
              this.$message.error(res.msg)
            }
          })
        })
        .catch(_ => {
          this.$message.info('取消删除')
        });
    }
  }
}
</script>

<style lang='less' scoped>
.export-excel-wrapper {
  padding: 0;
  margin: 0;
  float: right;
}
.position {
  // background: #fff;
  padding: 10px;
  // border: 1px solid #eee;
  margin-bottom: 10px;
}
.header {
  display: flex;
  background: #fff;
  padding: 10px;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
  .el-form-item {
    margin-bottom: 0;
  }
}
.wrapper {
  margin: 10px 0;
}
</style>